package com.somlsis.fragment

import android.util.Log
import com.base.BaseFragment
import com.somlsis.R
import com.somlsis.activity.BasicInfoActivity
import com.somlsis.databinding.FragmentBirthdayBinding
import com.widget.TimePickerCustom


class BirthdayFragment(private val listener: BasicInfoActivity.OnBasicInfoNextClickListener) :
    BaseFragment<FragmentBirthdayBinding>() {
    private var sex = 1
    private lateinit var timePickerCustom: TimePickerCustom
    override fun getViewBinding(): FragmentBirthdayBinding {
        return FragmentBirthdayBinding.inflate(layoutInflater)
    }

    override fun createView() {
        binding.selectDate.setOnClickListener {
            selectDate()
        }
        binding.male.setOnClickListener {
            sex = 1
            refreshSex()
        }
        binding.female.setOnClickListener {
            sex = 2
            refreshSex()
        }
        binding.next.setOnClickListener {
            val date = binding.selectDateText.text.toString().trim()
            listener.onBirthdayNextClick(date, sex)
        }
        createSelectDate()
    }

    override fun refreshUI() {
    }

    private fun createSelectDate(){
        timePickerCustom = TimePickerCustom(
            context, "选择生日",false
        ) { year: Int, month: Int, day: Int ->
            // 确定按钮的回调
            Log.i(
                "timePickerCustom",
                "$year------$month--------$day"
            )
            binding.selectDateText.text = "$year-$month"
        }
    }
    private fun selectDate() {
        if (timePickerCustom == null) {
            createSelectDate()
        }
            timePickerCustom.show()
    }

    private fun refreshSex() {
        if (sex == 1) {
            binding.male.setBackgroundResource(R.drawable.shape_ffe6edff_16)
            binding.female.setBackgroundResource(R.drawable.shape_ffffff_16)
        } else {
            binding.male.setBackgroundResource(R.drawable.shape_ffffff_16)
            binding.female.setBackgroundResource(R.drawable.shape_ffe6edff_16)
        }
    }

}